#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=200010;
int n,a[N];
int d[N];
ll sum,f[N],g[1000010],ans;
void solve(){
    cin>>n;
    int maxn=0;
    ans=0;sum=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    for(int i=1;i<=maxn;i++) g[i]=1e16;
    for(int i=2;i<=n;i++){
        if(a[i]==a[i-1]) d[i]=a[i];
        else d[i]=0;
        sum+=d[i];
    }
    // cout<<sum<<endl;
    f[1]=0;
    ll minn=0;
    for(int i=2;i<=n;i++){
        f[i]=minn+d[i];
        if(g[a[i]]!=1e16) f[i]=min(f[i],g[a[i]]-a[i]+d[i]);
        g[a[i-1]]=min(g[a[i-1]],f[i]);
        ans=min(ans,f[i]);
        // cout<<i<<' '<<f[i]<<endl;
        minn=min(minn,f[i]);
    }
    cout<<sum-ans<<endl;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
//100